//Like Aspect. Common DAO
const _ = require('lodash');
const uuidv4 = require('uuid/v4');
const fs = require('fs');
const userInfoModel = require('../../app/models/userInfo.json');

module.exports = {
    addUserInfo : addUserInfo,
    getUserInfoByUserId : getUserInfoByUserId
}

//Add user info
function addUserInfo(user, callback) {
    user.userInfoId = uuidv4();
    userInfoModel.push(user);
    var jsonString = JSON.stringify(userInfoModel, null, 4);
    fs.writeFile("./app/models/userInfo.json", jsonString, (err) => {
        if (err) throw err
        console.log('add success!');
    });
    callback(null, user);
}

//Get User info by userId 
function getUserInfoByUserId(userId, callback){
    let result = null;
    _.forEach(userInfoModel, (item) => {
        if (_.isEqual(userId, item.userId)) {
            result = item;
        }
    })
    callback(null, result);
}